package com.fottas.adware.portal.config;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SendSms {
    @Value("${alibaba.sms.SignName}")
    private String signName; //阿里云短信服务上创建的签名名称
    @Value("${alibaba.sms.TemplateCode}")
    private String templateCode;  //阿里云短信服务上创建的模板的模版CODE
    @Value("${alibaba.sms.AccessKeyID}")
    private String accessKeyId;
    @Value("${alibaba.sms.AccessKeySecret}")
    private String accessSecret;
    /**
     * @Title: sendSMS
     * @Description: 阿里云发送短信
     * @param  phone 电话号码
     * @param  code  自定义的验证码
     * @return void
     */
    public void sendSMS(String phone,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        //拼接模板参数（验证码）value
        String codeSMS="{\"code\":\""+code+"\"}";

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("SignName", signName);//签名必须使用阿里云短信服务上创建的签名名称
        request.putQueryParameter("TemplateCode", templateCode);//模板必须使用阿里云短信服务上创建的模板的模版CODE
        request.putQueryParameter("PhoneNumbers", phone);//电话号码
        request.putQueryParameter("TemplateParam", codeSMS);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

}